package com.macro.mall.tiny.modules.ums.controller.sensitive;

import com.macro.mall.tiny.common.api.CommonResult;
import com.macro.mall.tiny.modules.ums.model.bo.CourseWordBO;
import com.macro.mall.tiny.util.SensitiveWordUtil;

import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;

/**
 * @author: i_kun
 * @Desc: o_。
 * @create: 2025-06-27 22:48
 */
@Controller
@Tag(name = "SensitiveController",description = "敏感词管理")
@RequestMapping("/course")
public class SensitiveController {

    @Autowired
    private SensitiveWordUtil sensitiveWordUtil;
    // 初始化工具类（敏感词文件路径）
    @RequestMapping(value = "/check", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult check(@RequestBody String text){
        String filter2 = sensitiveWordUtil.filter(text);
        return CommonResult.success(filter2);
    }

    @RequestMapping(value = "/checkDemo", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult checkDemo(@RequestBody CourseWordBO courseWordBO){

        boolean isTrue = sensitiveWordUtil.containsSensitiveWord(courseWordBO.getText());
        if (isTrue){
            //脱敏
            String  filter = sensitiveWordUtil.filter(courseWordBO.getText());
            return CommonResult.success(filter);
        }
        return CommonResult.success(courseWordBO.getText());
    }

    @RequestMapping(value = "/addDemo", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult addDemo(@RequestBody CourseWordBO courseWordBO){
        for (String word : courseWordBO.getWordSet()) {
            System.out.println(word);
        }
        int num = sensitiveWordUtil.addSensitiveWords(courseWordBO.getWordSet());
        System.out.println("新增了" +num+"条敏感词");
        return CommonResult.success(true);
    }

    @RequestMapping(value = "/delDemo", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult delDemo(@RequestBody CourseWordBO courseWordBO){
        for (String word : courseWordBO.getWordSet()) {
            System.out.println(word);
        }
        int num = sensitiveWordUtil.removeSensitiveWords(courseWordBO.getWordSet());
        System.out.println("删除后还剩" +num+"条敏感词");
        return CommonResult.success(true);
    }


}
